<template>
  <div class="property-container">
    <!-- 加载动画 -->
    <div v-if="loading" class="loading-container">
      <div class="cyber-loading">
        <div class="hexagon"></div>
        <div class="loading-text">系统数据加载中...</div>
      </div>
    </div>

    <div v-else class="cyber-border">
      <div class="dashboard-header">
        <h2 class="cyber-title">物业空间管理</h2>
        <div class="stats-summary">
          <div class="stat-item">
            <span class="stat-value">{{ deviceStats.total }}</span>
            <span class="stat-label">设备总数</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ deviceStats.online }}%</span>
            <span class="stat-label">在线率</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ deviceStats.alerts }}</span>
            <span class="stat-label">告警数</span>
          </div>
        </div>
      </div>

      <!-- 空间网格布局 -->
      <div class="space-grid">
        <el-card 
          v-for="space in spaces" 
          :key="space.id"
          class="space-card"
          :class="{ 'alert': space.hasAlert }"
        >
          <div class="card-header">
            <h3>{{ space.name }}</h3>
            <el-tag :type="space.status === '占用' ? 'danger' : 'success'">
              {{ space.status }}
            </el-tag>
          </div>
          
          <div class="device-stats">
            <div class="stat">
              <span>设备总数</span>
              <span>{{ space.devices.length }}</span>
            </div>
            <div class="stat">
              <span>在线设备</span>
              <span>{{ space.onlineDevices }}/{{ space.devices.length }}</span>
            </div>
          </div>

          <div class="device-list">
            <div v-for="device in space.devices" 
                 :key="device.id" 
                 class="device-tag"
                 :class="{ 'offline': !device.online, 'alert': device.alert }">
              <i :class="device.icon"></i>
              {{ device.name }}
            </div>
          </div>
        </el-card>
      </div>

      <!-- 告警事件面板 -->
      <div class="alert-panel cyber-border">
        <div class="alert-header">
          <h3 class="cyber-subtitle">实时告警信息</h3>
          <div class="cyber-pagination">
            <button class="cyber-btn" 
                    :class="{ disabled: alertCurrentPage === 1 }"
                    @click="changePage(alertCurrentPage - 1)">
              上一页
            </button>
            <span class="page-info">{{ alertCurrentPage }} / {{ alertTotalPages }}</span>
            <button class="cyber-btn" 
                    :class="{ disabled: alertCurrentPage === alertTotalPages }"
                    @click="changePage(alertCurrentPage + 1)">
              下一页
            </button>
          </div>
        </div>
        <div class="alert-list">
          <div v-for="alert in currentAlerts" :key="alert.id" 
               class="alert-item" :class="alert.level">
            <div class="alert-icon">
              <i :class="alert.icon"></i>
            </div>
            <div class="alert-content">
              <div class="alert-header">
                <span class="alert-title">{{ alert.title }}</span>
                <span class="alert-time">{{ alert.time }}</span>
              </div>
              <div class="alert-desc">{{ alert.description }}</div>
              <div class="alert-location">位置：{{ alert.location }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'

const loading = ref(true)
const deviceStats = ref({
  total: 386,
  online: 98,
  alerts: 3
})

const spaces = ref([
  {
    id: 1,
    name: '主控机房',
    status: '在线',
    hasAlert: false,
    onlineDevices: 12,
    devices: [
      { id: 1, name: '核心服务器A', online: true, alert: false, icon: 'el-icon-cpu' },
      { id: 2, name: 'UPS系统-主', online: true, alert: false, icon: 'el-icon-lightning' },
      { id: 3, name: '环境监控主机', online: true, alert: false, icon: 'el-icon-monitor' },
      { id: 4, name: '安防主机', online: true, alert: false, icon: 'el-icon-video-camera' },
      { id: 5, name: '备用服务器', online: true, alert: false, icon: 'el-icon-cpu' }
    ]
  },
  {
    id: 2,
    name: '安防监控中心',
    status: '在线',
    hasAlert: true,
    onlineDevices: 28,
    devices: [
      { id: 6, name: '监控主机', online: true, alert: true, icon: 'el-icon-video-camera' },
      { id: 7, name: '门禁系统', online: true, alert: false, icon: 'el-icon-lock' },
      { id: 8, name: '人脸识别A区', online: true, alert: false, icon: 'el-icon-user' },
      { id: 9, name: '人脸识别B区', online: false, alert: false, icon: 'el-icon-user' }
    ]
  },
  {
    id: 3,
    name: '智能车库A区',
    status: '运行中',
    hasAlert: false,
    onlineDevices: 45,
    devices: [
      { id: 10, name: '车牌识别系统', online: true, alert: false, icon: 'el-icon-truck' },
      { id: 11, name: '智能闸机A1', online: true, alert: false, icon: 'el-icon-switch' },
      { id: 12, name: '车位引导系统', online: true, alert: false, icon: 'el-icon-guide' }
    ]
  },
  {
    id: 4,
    name: '能源控制中心',
    status: '告警',
    hasAlert: true,
    onlineDevices: 38,
    devices: [
      { id: 13, name: '智能电表系统', online: true, alert: true, icon: 'el-icon-lightning' },
      { id: 14, name: '光伏控制器', online: true, alert: false, icon: 'el-icon-sunny' },
      { id: 15, name: '储能系统', online: true, alert: false, icon: 'el-icon-battery' }
    ]
  },
  {
    id: 5,
    name: '智慧办公区',
    status: '在线',
    hasAlert: false,
    onlineDevices: 156,
    devices: [
      { id: 16, name: '智能照明', online: true, alert: false, icon: 'el-icon-light' },
      { id: 17, name: '空调系统', online: true, alert: false, icon: 'el-icon-cold' },
      { id: 18, name: '新风系统', online: true, alert: false, icon: 'el-icon-wind' }
    ]
  }
])

const mockSpaces = [
  {
    id: 1,
    name: '主控机房',
    status: '在线',
    hasAlert: false,
    onlineDevices: 12,
    devices: [
      { id: 1, name: '核心服务器A', online: true, alert: false, icon: 'el-icon-cpu' },
      { id: 2, name: 'UPS系统-主', online: true, alert: false, icon: 'el-icon-lightning' },
      { id: 3, name: '环境监控主机', online: true, alert: false, icon: 'el-icon-monitor' },
      { id: 4, name: '安防主机', online: true, alert: false, icon: 'el-icon-video-camera' },
      { id: 5, name: '备用服务器', online: true, alert: false, icon: 'el-icon-cpu' }
    ]
  },
  {
    id: 2,
    name: '安防监控中心',
    status: '在线',
    hasAlert: true,
    onlineDevices: 28,
    devices: [
      { id: 6, name: '监控主机', online: true, alert: true, icon: 'el-icon-video-camera' },
      { id: 7, name: '门禁系统', online: true, alert: false, icon: 'el-icon-lock' },
      { id: 8, name: '人脸识别A区', online: true, alert: false, icon: 'el-icon-user' },
      { id: 9, name: '人脸识别B区', online: false, alert: false, icon: 'el-icon-user' }
    ]
  },
  {
    id: 3,
    name: '智能车库A区',
    status: '运行中',
    hasAlert: false,
    onlineDevices: 45,
    devices: [
      { id: 10, name: '车牌识别系统', online: true, alert: false, icon: 'el-icon-truck' },
      { id: 11, name: '智能闸机A1', online: true, alert: false, icon: 'el-icon-switch' },
      { id: 12, name: '车位引导系统', online: true, alert: false, icon: 'el-icon-guide' }
    ]
  },
  {
    id: 4,
    name: '能源控制中心',
    status: '告警',
    hasAlert: true,
    onlineDevices: 38,
    devices: [
      { id: 13, name: '智能电表系统', online: true, alert: true, icon: 'el-icon-lightning' },
      { id: 14, name: '光伏控制器', online: true, alert: false, icon: 'el-icon-sunny' },
      { id: 15, name: '储能系统', online: true, alert: false, icon: 'el-icon-battery' }
    ]
  },
  {
    id: 5,
    name: '智慧办公区',
    status: '在线',
    hasAlert: false,
    onlineDevices: 156,
    devices: [
      { id: 16, name: '智能照明', online: true, alert: false, icon: 'el-icon-light' },
      { id: 17, name: '空调系统', online: true, alert: false, icon: 'el-icon-cold' },
      { id: 18, name: '新风系统', online: true, alert: false, icon: 'el-icon-wind' }
    ]
  }
]

// 告警事件数据
const alertEvents = ref([
  {
    id: 1,
    title: '温度超限警告',
    description: '主控机房温度达到28.5°C，超过预设阈值',
    location: '主控机房',
    level: 'warning',
    time: '2024-03-15 14:30:22',
    icon: 'el-icon-warning'
  },
  {
    id: 2,
    title: '设备离线告警',
    description: '人脸识别B区设备连接中断',
    location: '安防监控中心',
    level: 'error',
    time: '2024-03-15 14:28:15',
    icon: 'el-icon-error'
  },
  {
    id: 3,
    title: '电力负载异常',
    description: '智能电表系统检测到用电负载异常波动',
    location: '能源控制中心',
    level: 'critical',
    time: '2024-03-15 14:25:33',
    icon: 'el-icon-lightning'
  },
  {
    id: 4,
    title: 'UPS电池电量低',
    description: 'UPS系统备用电池电量低于20%',
    location: '主控机房',
    level: 'warning',
    time: '2024-03-15 14:20:45',
    icon: 'el-icon-battery'
  },
  {
    id: 5,
    title: '未授权访问',
    description: '检测到未授权人员尝试访问控制区域',
    location: '安防监控中心',
    level: 'critical',
    time: '2024-03-15 14:15:12',
    icon: 'el-icon-user'
  }
])

// 简化分页逻辑，只保留告警信息的分页
const itemsPerPage = 4
const alertCurrentPage = ref(1)

const alertTotalPages = computed(() => Math.ceil(alertEvents.value.length / itemsPerPage))

const currentAlerts = computed(() => {
  const start = (alertCurrentPage.value - 1) * itemsPerPage
  return alertEvents.value.slice(start, start + itemsPerPage)
})

const changePage = (page) => {
  if (page >= 1 && page <= alertTotalPages.value) {
    alertCurrentPage.value = page
  }
}

onMounted(async () => {
  // 模拟加载延迟
  setTimeout(() => {
    spaces.value = mockSpaces
    loading.value = false
  }, 2000)
})
</script>

<style lang="scss" scoped>
.property-container {
  padding: 20px;
  background-color: #0a1929;
  height: 100vh;
  overflow-y: auto;
  
  /* 自定义滚动条样式 */
  &::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  
  &::-webkit-scrollbar-track {
    background: rgba(13, 25, 48, 0.8);
    border-radius: 4px;
  }
  
  &::-webkit-scrollbar-thumb {
    background: rgba(76, 201, 240, 0.3);
    border-radius: 4px;
    
    &:hover {
      background: rgba(76, 201, 240, 0.5);
    }
  }
}

.cyber-border {
  background: rgba(13, 25, 48, 0.8);
  border: 1px solid rgba(32, 99, 155, 0.2);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px; // 确保底部有足够空间
}

.cyber-title {
  color: #4cc9f0;
  font-size: 2em;
  text-shadow: 0 0 10px rgba(76, 201, 240, 0.5);
  margin: 0;
}

.space-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.space-card {
  background: rgba(13, 25, 48, 0.9) !important;
  border: 1px solid rgba(76, 201, 240, 0.2) !important;
  
  .el-card__body {
    padding: 20px;
  }
  
  h3 {
    color: #4cc9f0;
    margin: 0;
    font-size: 1.2em;
    text-shadow: 0 0 5px rgba(76, 201, 240, 0.5);
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.device-stats {
  background: rgba(76, 201, 240, 0.1);
  padding: 10px;
  border-radius: 4px;
  margin: 10px 0;
  
  .stat {
    display: flex;
    justify-content: space-between;
    color: #4cc9f0;
    margin: 5px 0;
    font-size: 0.9em;
  }
}

.device-tag {
  background: rgba(76, 201, 240, 0.1);
  color: #4cc9f0;
  border: 1px solid rgba(76, 201, 240, 0.2);
  
  i {
    margin-right: 5px;
  }
  
  &.offline {
    background: rgba(255, 99, 132, 0.1);
    color: #ff6384;
    border-color: rgba(255, 99, 132, 0.2);
  }
  
  &.alert {
    background: rgba(255, 159, 64, 0.1);
    color: #ff9f40;
    border-color: rgba(255, 159, 64, 0.2);
    animation: blink 1s infinite;
  }
}

.stat-item {
  .stat-value {
    color: #4cc9f0;
    text-shadow: 0 0 10px rgba(76, 201, 240, 0.5);
  }
  
  .stat-label {
    color: #90caf9;
  }
}

.loading-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(10, 25, 41, 0.95);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.cyber-loading {
  text-align: center;
  
  .hexagon {
    width: 100px;
    height: 100px;
    margin: 0 auto;
    background: conic-gradient(from 0deg, #4cc9f0, #90caf9, #64b5f6, #4cc9f0);
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    animation: rotate 2s linear infinite;
  }
  
  .loading-text {
    margin-top: 20px;
    color: #4cc9f0;
    font-size: 1.2em;
    text-shadow: 0 0 10px rgba(76, 201, 240, 0.5);
  }
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.stats-summary {
  display: flex;
  gap: 20px;
}

.alert-panel {
  margin-top: 20px;
  margin-bottom: 20px;
  background: rgba(13, 25, 48, 0.9);
  border: 1px solid rgba(76, 201, 240, 0.2);
  border-radius: 8px;
  padding: 20px;
}

.alert-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.cyber-subtitle {
  color: #4cc9f0;
  font-size: 1.5em;
  margin: 0;
  text-shadow: 0 0 8px rgba(76, 201, 240, 0.5);
}

.alert-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.alert-item {
  display: flex;
  gap: 15px;
  padding: 15px;
  border-radius: 8px;
  background: rgba(13, 25, 48, 0.8);
  border: 1px solid rgba(76, 201, 240, 0.2);
  height: fit-content;
  
  &.warning {
    border-color: rgba(255, 193, 7, 0.3);
    .alert-icon { color: #ffc107; }
    .alert-title { color: #ffc107; }
    .alert-desc { color: rgba(255, 193, 7, 0.8); }
    .alert-location { color: rgba(255, 193, 7, 0.9); }
  }
  
  &.error {
    border-color: rgba(255, 99, 132, 0.3);
    .alert-icon { color: #ff6384; }
    .alert-title { color: #ff6384; }
    .alert-desc { color: rgba(255, 99, 132, 0.8); }
    .alert-location { color: rgba(255, 99, 132, 0.9); }
  }
  
  &.critical {
    border-color: rgba(255, 0, 0, 0.3);
    .alert-icon { color: #ff0000; }
    .alert-title { color: #ff0000; }
    .alert-desc { color: rgba(255, 0, 0, 0.8); }
    .alert-location { color: rgba(255, 0, 0, 0.9); }
    animation: blink 2s infinite;
  }
}

.alert-icon {
  font-size: 24px;
  display: flex;
  align-items: center;
}

.alert-content {
  flex: 1;
}

.alert-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.alert-title {
  font-weight: bold;
}

.alert-time {
  color: #90caf9;
  font-size: 0.9em;
}

.alert-desc {
  margin-bottom: 8px;
}

.alert-location {
  font-size: 0.9em;
}

.cyber-pagination {
  display: flex;
  align-items: center;
  gap: 15px;
  
  .cyber-btn {
    background: rgba(76, 201, 240, 0.1);
    border: 1px solid rgba(76, 201, 240, 0.3);
    color: #4cc9f0;
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
    
    &:hover:not(.disabled) {
      background: rgba(76, 201, 240, 0.2);
      border-color: rgba(76, 201, 240, 0.5);
      text-shadow: 0 0 5px rgba(76, 201, 240, 0.5);
    }
    
    &.disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
  }
  
  .page-info {
    color: #4cc9f0;
    font-size: 1.1em;
    text-shadow: 0 0 5px rgba(76, 201, 240, 0.3);
  }
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes blink {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}

// 响应式布局调整
@media screen and (max-width: 1200px) {
  .alert-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media screen and (max-width: 768px) {
  .alert-list {
    grid-template-columns: 1fr;
  }
  
  .space-grid {
    grid-template-columns: 1fr;
  }
  
  .stats-summary {
    flex-direction: column;
    gap: 10px;
  }
  
  .alert-header {
    flex-direction: column;
    gap: 15px;
    
    .cyber-subtitle {
      text-align: center;
    }
  }
}
</style> 